﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using phudieuhungthinh.BL;
using phudieuhungthinh.BL.Model;
using phudieuhungthinh.Helpers;

namespace phudieuhungthinh.Admin
{
    public partial class ProductItem : Page
    {
        public static string Copyright
        {
            get
            {
                string copyright = ConfigurationManager.AppSettings["copyright"];
                return copyright;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        [WebMethod]
        public static void UploadFile()
        {
            if (HttpContext.Current.Request.Files["Filedata"] != null)
            {
                HttpPostedFile fileUpload = HttpContext.Current.Request.Files["Filedata"];
                int index = fileUpload.FileName.LastIndexOf('.');
                string fileName = DateTime.Now.ToString("yyyyMMdd_HHmmss") + fileUpload.FileName.Substring(index);

                string originalPath = HttpContext.Current.Server.MapPath("~" + Define.ImageFolder) + fileName;
                string thumbPath = HttpContext.Current.Server.MapPath("~" + Define.ImageFolder + "Thumb/") + fileName;
                string copyrightPath = HttpContext.Current.Server.MapPath("~" + Define.ImageFolder + "Copyright/") +
                                       fileName;
                string tempName = Path.GetTempFileName();
                fileUpload.SaveAs(originalPath);

                // Save thumb
                var imgResizer = new ImageResizer {OutputFormat = ImageFormat.Jpeg, MaxWidth = 200, MaxHeight = 160};
                imgResizer.Resize(fileUpload, thumbPath);

                imgResizer.OutputFormat = ImageFormat.Jpeg;
                imgResizer.MaxWidth = 1020;
                imgResizer.MaxHeight = 900;
                imgResizer.Resize(originalPath, tempName);

                // Draw copy right
                var imgHelper = new ImageHelper();
                imgHelper.WriteCopyright(Copyright, string.Empty, tempName, copyrightPath);

                HttpContext.Current.Response.Write(fileName);
                FlushContext();
            }
        }

        private static void FlushContext()
        {
            HttpContext.Current.Response.Flush();
            HttpContext.Current.ApplicationInstance.CompleteRequest();
            HttpContext.Current.Response.SuppressContent = true;
        }


        [WebMethod(EnableSession = true)]
        public static object GetProduct(int id)
        {
            try
            {
                //Delete a product in database.
                var productImpl = new ProductImpl();
                ProductModel productModel = productImpl.GetProductInfo(id);

                return productModel;
            }
            catch
            {
                return null;
            }
        }

        [WebMethod(EnableSession = true)]
        public static object GetCategoryMenu(int productId)
        {
            try
            {
                var categoryImpl = new CategoryImpl();
                var m = new Menus {MenuList = categoryImpl.GetCategoryMenus(productId)};

                return m;
            }
            catch
            {
                return null;
            }
        }

        [WebMethod(EnableSession = true)]
        public static object GetCategories(int cateId)
        {
            try
            {
                var categoryImpl = new CategoryImpl();
                var m = new Menus {MenuList = categoryImpl.GetCategories(cateId)};

                return m;
            }
            catch
            {
                return null;
            }
        }


        [WebMethod(EnableSession = true)]
        public static bool SaveProduct(ProductModel productModel, List<string> arr)
        {
            try
            {
                var productCategoriesModelList = arr.Select(category => new ProductCategoriesModel
                {
                    CategoryID = int.Parse(category), 
                    ProductID = productModel.ID, 
                    UniqueID = Guid.NewGuid()
                }).ToList();
                var productImpl = new ProductImpl();
                if (productModel.ID != 0)
                {
                    productImpl.UpdateProduct(productModel, productCategoriesModelList);
                }
                else
                {
                    productModel.UniqueID = Guid.NewGuid();
                    productModel.IsActive = false;
                    productModel.Orders = 0;
                    productImpl.AddProduct(productModel, productCategoriesModelList);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}